#include "stdafx.h"
#include "standard/DBConfig.h"
#include "standard/nstring.h"
#include "standard/CThrowException.h"

DBConfig::DBConfig(void)
{
	m_filestr  = "";
}

DBConfig::~DBConfig(void)
{

}

void  DBConfig::readFile(std::string filename)
{
	FILE *fp;
	int len;
		
	fp = fopen(filename.c_str(),"rb");
	if(fp == NULL) {
	    std::cout<<"filename : "<<filename<<std::endl;
	    throw CThrowException("file not exists!. code 1001");
	}
	
	fseek(fp, 0L, SEEK_END);
	len = ftell(fp);
	char *buffer = new char[len+1];
	memset(buffer,0,len+1);
	rewind(fp);
	fread(buffer,1,len,fp);
	fclose(fp);	
	char *tembuf =new char[len+1];
	memset(tembuf,0,len+1);
	int j =0,i=0;
	while(buffer[i]!=0){
		if(buffer[i] =='\r'){
			i++;
			continue;
		}
		else
		   tembuf[j++] = buffer[i];
		i++;
	}
	m_filestr   = tembuf;
	delete[]tembuf;
	delete[]buffer;
}

std::string DBConfig::Getdbparam(std::string modulename,std::string param)
{
	std::string flagstr = "";
	char temp[64] = "";
	std::string bufstr = m_filestr; 

	if(modulename.compare("path") ==0) {
		sprintf(temp,"[%s]",modulename.c_str());
		flagstr =temp;
		int ncout = bufstr.find(flagstr.c_str());
		if(param.compare("pathname") == 0){
			int nhostfind = bufstr.find("pathname",ncout+1);
			int nbegin_ = bufstr.find("=",nhostfind+1);
			int nend_    = bufstr.find("\n",nbegin_+1);
			return bufstr.substr(nbegin_+2,nend_-nbegin_-2);
		}
		memset(temp,0,64);
	}

	sprintf(temp,"[db_%s]",modulename.c_str());
	flagstr = temp;
	int nfind =  bufstr.find(flagstr.c_str());
	if(nfind == -1)
		return NULL;
	if(param.compare("host") == 0)
	{
		int nhostfind = bufstr.find("host",nfind+1);
		int nbegin_ = bufstr.find("=",nhostfind+1);
		int nend_    = bufstr.find("\n",nbegin_+1);
		return bufstr.substr(nbegin_+2,nend_-nbegin_-2);

	}
	else if(param.compare("user") == 0)
	{
		int nuserfind = bufstr.find("user",nfind+1);
		int nbegin_ = bufstr.find("=",nuserfind+1);
		int nend_    = bufstr.find("\n",nbegin_+1);

		return bufstr.substr(nbegin_+2,nend_-nbegin_-2);
	}
	else if(param.compare("password") == 0)
	{
		int  npsfind = bufstr.find("password",nfind+1);
		int nbegin_ = bufstr.find("=",npsfind+1);
		int nend_    = bufstr.find("\n",nbegin_+1);

		return bufstr.substr(nbegin_+2,nend_-nbegin_-2);
	}
	else if(param.compare("name") == 0)
	{
		int nNamefind = bufstr.find("name",nfind+1);
		int nbegin_ = bufstr.find("=",nNamefind+1);
		int nend_    = bufstr.find("\n",nbegin_+1);

		return bufstr.substr(nbegin_+2,nend_-nbegin_-2);
	}
	else if(param.compare("charset") == 0)
	{
		int ncharsetfind = bufstr.find("charset",nfind+1);
		int nbegin_ = bufstr.find("=",ncharsetfind+1);
		int nend_    = bufstr.find("\n",nbegin_+1);
		return bufstr.substr(nbegin_+2,nend_-nbegin_-2);
	}
	else if(param.compare("prefix") == 0 )
	{
		int nprefixfind = bufstr.find("prefix",nfind+1);
		int nbegin_ = bufstr.find("=",nprefixfind+1);
		int nend_    = bufstr.find("\n",nbegin_+1);
		return bufstr.substr(nbegin_+2,nend_-nbegin_-2);
	}
	return NULL;
}
std::string DBConfig::Gethtmturn(std::string strname,std::string param)
{
    std::string bufstr = m_filestr; 
	char temp[64] = "";
	std::string flagstr = "";
	if(strname.compare("express") == 0)
	{
			sprintf(temp,"[%s]",strname.c_str());
			flagstr =  temp;
			int  s1 = bufstr.find(flagstr);
			if(s1 == -1){
				return NULL;
			}
			else{
				int begin_ = bufstr.find("=",s1+1);
				int end_ =  bufstr.find("(",begin_+1);
				std::string  compstr = bufstr.substr(begin_+1,end_-begin_-1);
				compstr = trim(compstr,0);
				int nfind = param.find(compstr);
				if(nfind == -1 ){
					return NULL;
				}
				else {
					return param.substr(nfind+compstr.length());
				}
			}
	}
	return NULL;
}
